package com.jt.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import redis.clients.jedis.Jedis;

@Configuration
@PropertySource("classpath:/properties/redis.properties")
public class RedisConfig {


     //redis集群
    @Value("${redis.nodes}")
    private String nodes;

    @Bean
    public JedisCluster jedisCluster(){
        Set<HostAndPort> nodeSet = new HashSet<>();
        String[] nodeArray = nodes.split(",");
        for( String node :nodeArray){  //node=host:port
            String host = node.split(":")[0];
            int port = Integer.parseInt(node.split(":")[1]);
            HostAndPort hostAndPort = new HostAndPort(host,port);
            nodeSet.add(hostAndPort);
        }
        JedisPoolConfig poolConfig = new JedisPoolConfig();
        poolConfig.setMaxTotal(1000);//	资源池中最大连接数
        poolConfig.setMaxIdle(40);//资源池允许最大空闲的连接数
        poolConfig.setMinIdle(10); //	资源池确保最少空闲的连接数
        return new JedisCluster(nodeSet,poolConfig);
    }


    //整合Redis哨兵
/*
    @Value("${redis.node}")
    private String node;

    @Bean
    public JedisSentinelPool jedisSentinelPool(){
        Set<String> sentinels = new HashSet<>();
        sentinels.add(node);
        JedisPoolConfig poolConfig = new JedisPoolConfig();
        poolConfig.setMaxTotal(1000);
        poolConfig.setMinIdle(40);
        poolConfig.setMaxIdle(60);
        return new JedisSentinelPool("mymaster",sentinels,poolConfig);
    }
*/


   /* @Value("${redis.nodes}") //redis实现分片机制
    private String nodes;   //node,node,node

    @Bean
    public ShardedJedis shardedJedis(){
        List<JedisShardInfo> shards = new ArrayList<>();
        String[] nodeArray = nodes.split(",");
        for( String node :nodeArray){  //node=host:port
            String host = node.split(":")[0];
            int port = Integer.parseInt(node.split(":")[1]);
            JedisShardInfo info = new JedisShardInfo(host,port);
            shards.add(info);
        }
        return new ShardedJedis(shards);
    }
*/



/*    @Value("${redis.host}")
    private  String host;
    @Value("${redis.port}")
    private  Integer port;

    @Bean
    public Jedis jedis(){
        return new Jedis(host,port);
    }
    */
    
}
